package com.vanzo.some;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by Administrator on 2017/5/22.\
 * 网络请求工具类
 */

public class RequestHttpURLConnectionUtils {

    private static String result = null;
    private static Drawable drawable;

    /**
     * get网络请求获取推荐位
     */
    public static String requestGetString(String requestUrl) throws Exception {

        if (TextUtils.isEmpty(requestUrl) || "".equals(requestUrl)) {
            return null;
        }

        HttpURLConnection urlConn = getHttpURLConnection(requestUrl);
//            LogUtils.d("状态码......:"+urlConn.getResponseCode());
        // 判断请求是否成功
        if (urlConn.getResponseCode() == 200) {
            // 获取返回的数据
            result = streamToString(urlConn.getInputStream());
        }
        // 关闭连接
        urlConn.disconnect();


        return result;
    }


    /**
     * 获取推荐为对应的apk图片
     */
    public static Drawable requestGetDrawable(String requestUrl, Context context) {

        //查看本地缓存中是否有图片
        try {
            HttpURLConnection urlConn = getHttpURLConnection(requestUrl);
            Bitmap bitmap = BitmapFactory.decodeStream(urlConn.getInputStream());

            drawable = new BitmapDrawable(bitmap);

        } catch (IOException e) {
            e.printStackTrace();
        }


        return drawable;
    }


    /**
     * 网络请求的设置
     */
    @NonNull
    private static HttpURLConnection getHttpURLConnection(String requestUrl) throws IOException {
        // 新建一个URL对象
        URL url = new URL(requestUrl);
        // 打开一个HttpURLConnection连接
        HttpURLConnection urlConn = null;
        urlConn = (HttpURLConnection) url.openConnection();
        // 设置连接主机超时时间
        urlConn.setConnectTimeout(5 * 1000);
        //设置从主机读取数据超时
        urlConn.setReadTimeout(5 * 1000);
        // 设置是否使用缓存  默认是true
        urlConn.setUseCaches(true);
        // 设置为get请求
        urlConn.setRequestMethod("GET");

        //urlConn设置请求头信息
        //设置请求中的媒体类型信息。
        //urlConn.setRequestProperty("Content-Type", "application/json");
        //设置客户端与服务连接类型
        //urlConn.addRequestProperty("Connection", "Keep-Alive");
        urlConn.setRequestProperty("Accept-Charset", "utf-8");
        urlConn.setRequestProperty("contentType", "utf-8");
        urlConn.setRequestProperty("Content-type", "text/html");
        // 开始连接
        urlConn.connect();
        return urlConn;
    }

    /**
     * 将输入流转换成字符串
     */
    private static String streamToString(InputStream is) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = is.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            baos.close();
            is.close();
            byte[] byteArray = baos.toByteArray();
            return new String(byteArray);
        } catch (Exception e) {
            Log.e("YaoFang", e.toString());
            return null;
        }
    }

    /**
     * 检查是否有网
     */
    public static boolean checkNetWork(Context context) {
        ConnectivityManager netWork = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = netWork.getActiveNetworkInfo();
        if (info != null && info.isConnected()) {
            //有网
            return true;
        } else {
            //没有网
            return false;
        }
    }
}
